package com.dq.security.security;

import com.dq.security.entity.User;
import com.dq.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Objects;

/**
 * @ClassName UserDetailsServiceImpl
 * @Description 用户查询权限
 * @Author DuX1aoQiang
 * @Date 2023/5/25 17:31
 * @Version 1.0
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) {

        User user = userService.queryUserByUsername(username);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户查找失败");
        }
        org.springframework.security.core.userdetails.User userDetail = new org.springframework.security.core.userdetails.User(user.getName(), user.getPassword(), getAuthority(user.getId()));
        return userDetail;
    }



    private Collection<? extends GrantedAuthority> getAuthority(Long id) {
        String userAuthorities =  userService.getUserAuthorities(id);
        return null;
    }
}
